#include "gabaritoAux.h"

using namespace cv;
using namespace std;




void help()
{
    cout << "\nThis program demonstrates line finding with the Hough transform.\n"
            "Usage:\n"
            "./houghlines <image_name>, Default is pic1.png\n" << endl;
}

int main(int argc, char** argv)
{

    const char* filename = argc >= 2 ? argv[1] : "CV07.jpg";

    Mat img_original = imread(filename, 0);
    if(img_original.empty())
    {
        help();
        cout << "can not open " << filename << endl;
        return -1;
    }
    //Mat i = Mat()


    Mat img_canny, cdst;
    Canny(img_original, img_canny, 50, 200, 3);
    cvtColor(img_canny, cdst, CV_GRAY2BGR);

    vector<Vec4i> lines;
    HoughLinesP(img_canny, lines, 1, CV_PI/180, 50, 50, 10 );


    for( size_t i = 0; i < lines.size(); i++ )
    {
        Vec4i l = lines[i];
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 1, CV_AA);
    }

    double angulo = arctangente(lines[0][0],lines[0][1],lines[0][2],lines[0][3])*180/CV_PI;
    cout <<" angulo: " << angulo << endl;
    Vec4i bordas;

    Mat canny_rodada = rotateImage(img_canny,angulo-90);
    bordas = detectaExtremos(canny_rodada);
    //cout<<"\t bordas:\nCima="<<bordas[0]<<"\nEsquerda="<<bordas[1]<<"\nDireita="<<bordas[2]<<"\nBaixo="<<bordas[3]<<endl;




    //imshow("canny", canny_rot);
    Mat rodada = rotateImage(img_original,angulo-90.0);
    Mat cortada = cortaImagem(rodada,bordas);
    Mat redimensionada ;
    Size2i sz = Size2i(480,640);
    resize(cortada,redimensionada,sz,0,0,INTER_CUBIC);
    cout << "largura: " << redimensionada.cols <<",altura: " << redimensionada.rows<<endl;
    Mat final=redimensionada.clone();
    if(cabeca_pra_baixo(redimensionada))
       inverteImagem(redimensionada,final);


    imshow("detected lines", final);

    waitKey();

    return 0;
}






